package com.mjh.zhujianju.controller.visit;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.livew.framework.base.controller.BaseController;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedResult;
import com.livew.framework.pojo.AjaxResult;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.model.visit.VisitUserEO;
import com.mjh.zhujianju.service.visit.VisitUserService;
import com.mjh.zhujianju.vo.visit.VisitUserVO;

@Controller
@RequestMapping("visit/user")
public class VisitUserController extends BaseController<VisitUserEO> {

	@Resource
	private VisitUserService visitUserService;

	@Override
	protected BaseService<VisitUserEO> getService() {

		return visitUserService;
	}

	/**
	 * 保存
	 * 
	 * @Description:
	 * @param vo
	 * @return
	 * @author wt1293
	 * @date 2018年8月21日下午1:46:27
	 *
	 */
	@RequestMapping(value = "saveInfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult saveInfo(VisitUserVO vo) {
		if (vo == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			visitUserService.saveInfo(vo);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE, "保存成功");
	}

	/**
	 * 
	 * @Description: 更新
	 * @param vo
	 * @return
	 * @author wt1293
	 * @date 2018年8月21日下午1:46:14
	 *
	 */

	@RequestMapping(value = "updateInfo", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult updateInfo(VisitUserVO vo) {
		if (vo == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			visitUserService.updateInfo(vo);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE, "保存成功");
	}

	/**
	 * 分页
	 * 
	 * @Description:
	 * @return
	 * @author wt1293
	 * @date 2018年8月9日下午8:08:53
	 *
	 */
	@RequestMapping(value = "getPage", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult getPage(VisitUserVO vo) {
		PagedResult<VisitUserVO> list = visitUserService.getVOPage(vo);
		return AjaxResult.getResult(list);
	}

	/**
	 * 获取详情
	 */
	public AjaxResult findById(@PathVariable("id") Long id) {
		if (id == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		VisitUserVO t = this.visitUserService.getInfo(id);
		return AjaxResult.getResult(t);
	}

	@RequestMapping(value = "exportExcel", method = RequestMethod.GET)
	public void exportExcel(String remark, HttpServletResponse response) throws IOException {
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("信息表");
		VisitUserVO vo = new VisitUserVO();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		String time = sdf.format(new Date());
		vo.setRemark(remark);
		List<VisitUserVO> list = visitUserService.getVOList(vo);
		String fileName = "信访用户" +time+ ".xls";// 设置要导出的文件的名字
		int rowNum = 1;
		String[] headers = { "序号","姓名", "性别", "身份证号", "手机号", "所在地", "所在地", "信访次数" };
		// headers表示excel表中第一行的表头
		HSSFRow row = sheet.createRow(0);

		for (int i = 0; i < headers.length; i++) {
			HSSFCell cell = row.createCell(i);
			HSSFRichTextString text = new HSSFRichTextString(headers[i]);
			cell.setCellValue(text);
		}
		if (!XTListUtils.isEmpty(list)) {
			// 在表中存放查询到的数据放入对应的列
			for (VisitUserVO v : list) {
				HSSFRow row1 = sheet.createRow(rowNum);
				row1.createCell(0).setCellValue(rowNum);
				row1.createCell(1).setCellValue(v.getName());
				row1.createCell(2).setCellValue(v.getSexName());
				row1.createCell(3).setCellValue(v.getIdCard());
				row1.createCell(4).setCellValue(v.getPhone());
				row1.createCell(5).setCellValue(v.getProvinceName() + v.getCityName() + v.getCountyName());
				row1.createCell(6).setCellValue(v.getDetailAddress());
				row1.createCell(7).setCellValue(v.getNumber());
				rowNum++;
			}
		}
		response.setContentType("application/octet-stream");
		response.setHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1" ));
		response.flushBuffer();
		workbook.write(response.getOutputStream());
	}
}
